package com.etonedu.meals.base;

import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;


import androidx.fragment.app.Fragment;

import com.etonedu.meals.enumeration.MessageEnum;
import com.etonedu.meals.utils.SharedPreferencesUtil;

import org.greenrobot.eventbus.EventBus;
import org.xutils.x;

/**
 * Created by yf7429 on 2016/11/2.
 */

public class BaseFragment extends Fragment {

    protected String TAG = getClass().getSimpleName();
    public SystemBaseFragment systemBaseFragment;
    public SharedPreferencesUtil spu;
    public Toast mToast;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getActivity() != null) {
            this.systemBaseFragment = SystemBaseFragment.instance;
            spu = SharedPreferencesUtil.getInstance(getActivity());
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    protected int getResourceId(String resourceName) {
        Resources resources = getActivity().getResources();
        int resourceId = resources.getIdentifier(resourceName, "mipmap", getActivity().getPackageName());
        return resourceId;
    }



    public int delayTime = 3 * 60 * 1000;
    public Handler handler = new Handler(x.app().getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            Looper.prepare();
            super.handleMessage(msg);
        }
    };

    public Runnable runnable = new Runnable() {
        @Override
        public void run() {
            EventBus.getDefault().postSticky(MessageEnum.NONE);
        }
    };

    protected void showToast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(x.app(), msg, Toast.LENGTH_LONG);
        } else {
            mToast.setText(msg);
            mToast.setDuration(Toast.LENGTH_LONG);
        }
        mToast.show();
    }

    public void notifyData() {

    }
}